package com.cdream.common;

import com.cdream.common.idempotent.autoconfig.IdempotentProperties;
import com.cdream.common.idempotent.core.IdempotentStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author cdream
 * @date 2023/1/4
 */
@Slf4j
@RequiredArgsConstructor
public class RedisIdempotentStrategyImpl implements IdempotentStrategy {
    private final StringRedisTemplate redisTemplate;
    private final IdempotentProperties properties;

    @Override
    public void save(String uniqueKey) {
        redisTemplate.opsForValue().set(uniqueKey, properties.getUniqueValue(),
                properties.getKeyTimeOut(), properties.getTimeOutTimeUnit());
    }


    @Override
    public boolean exitKey(String key) {
        return redisTemplate.opsForValue().get(key) != null;
    }
}
